package com.dbappsecurity.jsbridge.api;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.webkit.JavascriptInterface;
import com.dbappsecurity.jsbridge.contract.JsApiBaseContract;
import com.dbappsecurity.jsbridge.entity.jsCommunicate;
import com.dbappsecurity.jsbridge.entity.jsGroupItems;
import com.dbappsecurity.jsbridge.webview.CompletionHandler;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public class JsApiBase {

  private JsApiBaseContract mContract;
  private Handler mHandler = new Handler(Looper.getMainLooper());
  private Gson gson = new Gson();

  public JsApiBase(JsApiBaseContract contract) {
    mContract = contract;
  }

  @JavascriptInterface
  public void toast(Object arg, CompletionHandler handler) throws JSONException {
    JSONObject jsonObject = (JSONObject) arg;
    String content = jsonObject.getString("content");
    mHandler.post(() -> mContract.toast(content, handler));
  }

  @JavascriptInterface
  public void statusBar(Object arg, CompletionHandler handler) throws JSONException {
    JSONObject jsonObject = (JSONObject) arg;
    String  color = jsonObject.getString("color");
    mHandler.post(() -> mContract.statusBar(color));
  }

  @JavascriptInterface
  public void menu(Object arg, CompletionHandler handler) {
    JSONObject jsonObject = (JSONObject) arg;
    final jsGroupItems items = gson.fromJson(jsonObject.toString(), jsGroupItems.class);
    mHandler.post(() -> mContract.menu(items.getTitle(), items.getItemList(), handler));
  }

  @JavascriptInterface
  public void communicate(Object arg, CompletionHandler handler) {
    JSONObject jsonObject = (JSONObject) arg;
    final jsCommunicate communicate = gson.fromJson(jsonObject.toString(), jsCommunicate.class);
    mHandler.post(
        () -> mContract.communicate(communicate.type, communicate.num, communicate.content));
  }

  @JavascriptInterface
  public void longPress(Object arg, CompletionHandler handler) {
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiBase", "Arirus communicate: " + jsonObject);
    mHandler.post(() -> {
      try {
        mContract.longPress(jsonObject.getString("type"), jsonObject.getString("src"));
      } catch (JSONException e) {
        e.printStackTrace();
      }
    });
  }

  @JavascriptInterface
  public void deviceInfo(Object arg, CompletionHandler handler) {
    mHandler.post(() -> mContract.deviceInfo(handler));
  }

  @JavascriptInterface
  public void appInfo(Object arg, CompletionHandler handler) {
    mHandler.post(() -> mContract.appInfo(handler));
  }

  @JavascriptInterface
  public void download(Object arg, CompletionHandler handler) {
    JSONObject jsonObject = (JSONObject) arg;
    Log.i("JsApiBase", "Arirus communicate: " + jsonObject);
    mHandler.post(() -> {
      try {
        mContract.download(jsonObject.getString("url"), jsonObject.getString("tag"));
      } catch (JSONException e) {
        e.printStackTrace();
      }
    });
  }

  @JavascriptInterface
  public void exit(Object arg, CompletionHandler handler) {
    mHandler.post(() -> mContract.exit());
  }

  @JavascriptInterface
  public void setSession(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    try {
      final Map<String, String> map =
          gson.fromJson(jsonObject.get("data").toString(), HashMap.class);
      mHandler.post(() -> mContract.setSession(map, handler));
    } catch (Exception w) {
      w.printStackTrace();
    }
  }

  @JavascriptInterface
  public void getSession(Object arg, CompletionHandler handler) {
    if (arg == null) return;
    JSONObject jsonObject = (JSONObject) arg;
    try {
      final List<String> list = gson.fromJson(jsonObject.get("keys").toString(), ArrayList.class);
      mHandler.post(() -> mContract.getSession(list, handler));
    } catch (Exception w) {
      w.printStackTrace();
    }
  }

  public void showPdf(String url, CompletionHandler handler) {

  }
}
